<?php

namespace app\admin\controller;

use app\common\controller\Backend;

use think\Controller;
use think\Request;
use JPush\Client as JPush;

/**
 * 行程报名名单
 *
 * @icon fa fa-circle-o
 */
class Travelmember extends Backend
{
    
    /**
     * TravelMember模型对象
     */
    protected $model = null;
    protected $travel_id = 0;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('TravelMember');
        $this->travel_id = input('travel_id');
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
     * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->where($where)
                    ->where('travel_id',$this->travel_id)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->where($where)
                    ->where('travel_id',$this->travel_id)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();
            foreach ($list as $k => $v) {
                $list[$k]['type'] = model('travel')->where(['id'=>$v['travel_id']])->value('type');
            }
            $list = addtion($list, [
            [
                'field'    => 'member_id',
                'display'  => 'user_name',
                'primary'  => 'id',
                'column'   => 'name',
                'model'    => '\app\common\model\User',
                'name'     => 'User',
                'table'    => 'User'
            ],
            [
                'field'    => 'parentid',
                'display'  => 'parent_name',
                'primary'  => 'id',
                'column'   => 'name',
                'model'    => '\app\common\model\User',
                'name'     => 'User',
                'table'    => 'User'
            ]
            ]);
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    // 海外游学 尾款支付推送
    public function remind()
    {
        if ($this->request->isAjax())
        {
            $tm_id = input('tmid',0);
            $travelmember = model('TravelMember')->get($tm_id);
            $user = model('User')->get($travelmember['member_id']);
            $client = new JPush(config('JPush.app_key'), config('JPush.master_secret'));
            $pusher = $client->push();
            $pusher->setPlatform('all');
           // $pusher->setPlatform('ios');
            //$pusher->addAlias(array('15052760816'));
            $pusher->addAlias(array($user['mobile']));
            //$pusher->addTag(array('tag1', 'tag2'))
            //$pusher->addAllAudience();
            $pusher->setNotificationAlert('您的海外游学申请手续办理成功，请登录APP支付尾款');
            try {
                $pusher->send();
            } catch (\JPush\Exceptions\JPushException $e) {
                // try something else here
                return json(['code'=>2,'msg'=>'发送失败']);
            }
            
            return json(['code'=>1,'msg'=>'发送成功']);
        }
    }

    // 详情
    /**
     * 供应商详情，根据身份不同显示不同的内容
     */
    public function detail($tmid)
    {   
        $otherstr = $this->model::where('id',$tmid)->value('other');
        $other = [];
        foreach (explode(',', $otherstr) as $k => $v) {
            $info = model('TravelOther')->get($v);
            array_push($other, ['name'=>$info['name'],'fee'=>$info['fee']]);
           // $row[$k]['other'] = ['name'=>$info['name'],'fee'=>$info['fee']];
        }
        $this->view->assign("row", $other);
        return $this->view->fetch();
    }

}
